﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using School.Entities;
using System.Collections;
using System.IO;
using School.Admission.ReportsCls;

namespace School.Admission
{
    public class Test_Appointment
    {
        private static SchoolEntities db = new SchoolEntities();

        public static Int64 Add(ADM_Test_Appointment Appointment)
        {
            try
            {
                
                db.AddToADM_Test_Appointment(Appointment);

                db.SaveChanges();
                return Appointment.Id;
            }
            catch
            {
                return -1;
            }
        }
        public static ADM_Test_Appointment Single(long id)
        {
            try
            {
                return db.ADM_Test_Appointment.Single(x => x.Id == id);
            }
            catch
            {
                return null;
            }
        }


        public static IEnumerable<ADM_Test_Appointment> All(Hashtable items)
        {
            SchoolEntities db = new SchoolEntities();

            var query = from Test in db.ADM_Test_Appointment
                        select Test
                        ;
            foreach (DictionaryEntry item in items)
            {
                if (item.Key == "Student_Id")
                {
                    long Student_Id;
                    Student_Id = Convert.ToInt64(item.Value);
                    query = query.Where(c => c.Student_Id == Student_Id);
                }
                if (item.Key == "Test_Id")
                {
                    long Test_Id;
                    Test_Id = Convert.ToInt64(item.Value);
                    query = query.Where(c => c.Test_Id == Test_Id);
                }
                if (item.Key == "Teacher_Id")
                {
                    long Teacher_Id;
                    Teacher_Id = Convert.ToInt64(item.Value);
                    query = query.Where(c => c.Teacher_Id == Teacher_Id);
                }


                if (item.Key == "Degree")
                {
                    Single Degree;
                    Degree = Convert.ToSingle(item.Value);
                    query = query.Where(c => c.Degree == Degree);
                }
                if (item.Key == "EductionalYear_Id")
                {
                    long EductionalYear_Id;
                    EductionalYear_Id = Convert.ToInt64(item.Value);
                    query = query.Where(c => c.EductionalYear_Id == EductionalYear_Id);
                }
                if (item.Key == "Date")
                {
                    DateTime myDate;
                    myDate = Convert.ToDateTime(item.Value);
                    query = query.Where(c => c.Date == myDate);
                }
                if (item.Key == "Time")
                {
                    DateTime myDate;
                    myDate = Convert.ToDateTime(item.Value);
                    query = query.Where(c => c.Time == myDate);
                }
                if (item.Key == "Passed")
                {
                    query = query.Where(c => c.TE_Student.Status_Id == 1 || c.TE_Student.Status_Id == 3);
                }
            }
            return query.ToList();
        }

        public static void Update(ADM_Test_Appointment Appointment)
        {

            ADM_Test_Appointment c = db.ADM_Test_Appointment.Single(x => x.Id == Appointment.Id);
            c.Student_Id = Appointment.Student_Id;
            c.Test_Id = Appointment.Test_Id;
            c.Teacher_Id = Appointment.Teacher_Id;

            c.Date = Appointment.Date;
            c.Time = Appointment.Time;
            c.Degree = Appointment.Degree;
            c.Comment = Appointment.Comment;
            c.EductionalYear_Id = Appointment.EductionalYear_Id;
            db.SaveChanges();

        }

        public static Boolean Delete(long id)
        {
            try
            {
                db.ADM_Test_Appointment.DeleteObject(db.ADM_Test_Appointment.Single(x => x.Id == id));
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static Boolean checkOldApp(long Test_Id)
        {
            try
            {
                IEnumerable<ADM_Test_Appointment> Results =  from Test in db.ADM_Test_Appointment
                                                             where Test.Test_Id==Test_Id 
                                                                select Test;

                db.ExecuteStoreCommand ("Delete from ADM_Test_Appointment Where Test_Id = " + Test_Id);


                //foreach (ADM_Test_Appointment TestApp in Results )
                //{
                //db.ADM_Test_Appointment.DeleteObject(db.ADM_Test_Appointment.Single  (x => x.Id == TestApp.Id));
                //db.ADM_Test_Appointment.deleteOnsubmit();

                //}
                //IEnumerable<ADM_Student_Subject> Results2 = from Test in db.ADM_Student_Subject
                //                                            where Test.Test_Id == Test_Id
                //                                            select Test;
                //foreach (ADM_Student_Subject TestResult in Results2)
                //{
                    //db.ADM_Student_Subject.DeleteObject(db.ADM_Student_Subject.Single(x => x.Id == TestResult.Id));
                db.ExecuteStoreCommand("Delete from ADM_Student_Subject Where Test_Id = " + Test_Id);
                //}
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static IEnumerable<ADM_Test_Appointment> All()
        {
            SchoolEntities db = new SchoolEntities();

            var query = from Test in db.ADM_Test_Appointment

                        select Test;
            return query.ToList();
        }
        public IQueryable<rptTestAppoinmentcls> GetStudentsTestApp(int? Id)
        {
            var query = from StudentTest in db.ADM_Test_Appointment 
                        where StudentTest.Test_Id  == Id
                        select new ReportsCls.rptTestAppoinmentcls
                        {
                            studentFullName = StudentTest.TE_Student.Ar_FirstName + " " + StudentTest.TE_Student.Ar_SecondName + " " + StudentTest.TE_Student.Ar_ThirdNmae + " " + StudentTest.TE_Student.Ar_FourthName,
                            Campus = StudentTest.ADM_Test.ORG_Campus.Name,
                            Certificate = StudentTest.ADM_Test.ORG_Certificate.Name,
                            Stage = StudentTest.ADM_Test.ORG_Stage.Name,
                            Grade = StudentTest.ADM_Test.ORG_Grade.Name,
                            TestDate = StudentTest.Date,
                            TestTime = StudentTest.Time,
                            TeacherName = StudentTest.ORG_Stuff_Teacher.ORG_Stuff.FName + " " + StudentTest.ORG_Stuff_Teacher.ORG_Stuff.MName + " " + StudentTest.ORG_Stuff_Teacher.ORG_Stuff.LName ,
                            studentFileNumber = StudentTest.TE_Student.FileNumber

                        };
            
            return query;

        }
    }
}
